#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string>

#include "common.hpp"

int main()
{
    //打开管道文件准备写
    int fd = open(FIFO_FILE, O_WRONLY);
    if(fd == -1)
    {
        std::cerr << "open fifo fail." << std::endl;
    }
    else
    {
        std::cout << "open fifo success." << std::endl;
    }

    //写入内容
    while(true)
    {
        std::string message;
        std::cout << "Please enter# " << std::endl;
        std::getline(std::cin, message);
        int cnt = 1;
        pid_t id = getpid();
        message +=  ", message number: " + std::to_string(cnt) + ", pid: [" + std::to_string(id) + "]";
        int n = write(fd, message.c_str(), message.size());
        if(n == -1)
        {
            std::cerr << "write fail" << std::endl;
        }
        else
        {
            std::cout << "write success" << std::endl;
        }
    }

    //关闭管道文件
    close(fd);
    return 0;
}